package demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class CardGame {
    public static final String[] suits = {"♥","♦","♣","♠"};
    //买牌
    public List<Card> buyCard() {
        List<Card> cardList = new ArrayList<>();
        for(int i = 0;i < suits.length;i++) {
            for(int j = 1;j <= 13;j++) {
                //产生
                String suit = suits[i];
                Card card = new Card(suit,j);
                //添加
                cardList.add(card);
                //cardList.add(new Card(suits[i],j))
            }
        }
        return cardList;
    }
    //洗牌
    public void shuffle(List<Card> cardList) {
        //随机数,进行交换
        Random random = new Random();
        for(int i = cardList.size() - 1;i > 0;i--) {
            //生成随机数,传值
            int index = random.nextInt(i);
            swap(cardList,i,index);
        }
    }
    private void swap(List<Card> cardList,int i,int j) {
        Card tmp = cardList.get(i);//获取
        cardList.set(i,cardList.get(j));//交换位置
        cardList.set(j,tmp);
    }
    //揭牌

    /**
     * 3个人,一人共5张
     * @param cardList
     */
    public List<List<Card>> getCard(List<Card> cardList) {
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hand = new ArrayList<>();//二维数组
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);

        for(int i = 0; i < 5;i++) {
            for(int j = 0;j < 3;j++) {
                //移动第一张,后面值覆盖即可,调用remove
                Card card = cardList.remove(0);
                hand.get(j).add(card);
            }
        }
        return hand;
    }
}
